package org.noote.libs.roby.comm;

import org.noote.libs.tcpip.IPEvent_Server;
import org.noote.libs.tcpip.IPServer_ConnectedThread;

import android.util.Log;

//////////////////////////////////
///
/// 
///
/////////////////////////////////

public class IPServerCommandEvents implements IPEvent_Server,IPServerCommandEvents_Interface {
	static final String TAG = "IPServerCommandHandler";
	private boolean _isDebug = false;
	IPServer_ConnectedThread _thread;

	@Override
	public void setThread(IPServer_ConnectedThread thread)
	{
		this._thread = thread;  
	}
	
	protected void sendMessage(String sMsg) {
		_thread.sendMessage(sMsg);
	}

	@Override
	public void onConnected() {
	}
	@Override
	public void onDisconnected() {
	}
	@Override
	public void onError(String sError) {
	}
	@Override
	public void onText(String sText) {
	}
	@Override
	public void onCmd(String sMessage)
	{
    	if(_isDebug)
    		Log.d(TAG, "Get cmd : "+sMessage);
		String [] sCmds = sMessage.split("\\:");
		if(sCmds.length==2)
		{
			String sCmd = sCmds[0].trim().toLowerCase();
			String sOps = sCmds[1].trim().toLowerCase();
        	if(sCmd.equals("error"))
        	{
        		if(sOps.length()>0)
        			onError(sOps);
        	}
        	else if(sCmd.equals("message"))
        	{
        		if(sOps.length()>0)
        			onText(sOps);
        	}
        	else if(sCmd.equals("quit"))
        	{
        		onDisconnected();
        	}
        	else if(sCmd.equals("version"))
        	{
    			onAskVersion();
        	}
        	else if(sCmd.equals("version_hello"))
        	{
        		String sVersion="", sName="";
        		
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("version"))
        				sVersion = sValues[1];
        			else if(sValues[0].equals("name"))
        				sName = sValues[1];
        		}
    			onHello(sVersion, sName);
        	}
        	else if(sCmd.equals("setting_ask"))
        	{
        		if(sOps.equals("body"))
        			onAskSettingBody();
        		else if(sOps.equals("turret"))
        			onAskSettingTurret();
        		else if(sOps.equals("ray"))
        			onAskSettingRay();
        		else
        			onAskSetting();
        	}
        	else if(sCmd.equals("setting_set"))
        	{
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("ray_count"))
        			{
        				onSetSettingRayCount(Integer.getInteger(sValues[1]));
        			}
        			else if(sValues[0].equals("ray_delay"))
        			{
        				onSetSettingRayDelay(Integer.getInteger(sValues[1]));
        			}
        			else if(sValues[0].equals("ray_sensors"))
        			{
        				onSetSettingRaySensors(Integer.getInteger(sValues[1]));
        			}
        			else if(sValues[0].equals("ray_precision"))
        			{
        				onSetSettingRayPrecision((float)Integer.getInteger(sValues[1])/100.0f);
        			}
        		}
        	}
        	else if(sCmd.equals("status_ask"))
        	{
        		if(sOps.equals("sensors"))
        			onAskStatusSensors();
        		else if(sOps.equals("turret"))
        			onAskStatusTurret();
        		else if(sOps.equals("body"))
        			onAskStatusBody();
        		else
        			onAskStatus();
        	}
        	else if(sCmd.equals("status_set"))
        	{
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("send"))
        			{
        				onSetSendStatus((Integer.getInteger(sValues[1])==1)?true:false);
        			}
        		}
        	}
        	else if(sCmd.equals("reset"))
        	{
        		if(sOps.equals("body"))
        			onResetBody();
        		else if(sOps.equals("turret"))
        			onResetTurret();
        		else
        			onReset();
        	}
        	else if(sCmd.equals("body_move"))
        	{
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("front"))
        				onBodyFront(Integer.getInteger(sValues[1]));
        			else if(sValues[0].equals("back"))
        				onBodyBack(Integer.getInteger(sValues[1]));
        			else if(sValues[0].equals("left"))
        				onBodyLeft(Integer.getInteger(sValues[1]));
        			else if(sValues[0].equals("right"))
        				onBodyRight(Integer.getInteger(sValues[1]));
        		}
        	}
        	else if(sCmd.equals("body_target_move"))
        	{
        		int iSpeed = 0, iLen = 0;
        		boolean bFront = false;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("front"))
        				bFront = Integer.getInteger(sValues[1])==1?true:false;
        			else if(sValues[0].equals("speed"))
        				iSpeed = Integer.getInteger(sValues[1]);
        			else if(sValues[0].equals("len"))
        				iLen = Integer.getInteger(sValues[1]);
        		}
        		onBodyMoveToTarget(iSpeed, bFront, iLen);
        	}
        	else if(sCmd.equals("body_target_rotate"))
        	{
        		int iSpeed = 0, iBearing = 0;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("bearing"))
        				iBearing = Integer.getInteger(sValues[1]);
        			else if(sValues[0].equals("speed"))
        				iSpeed = Integer.getInteger(sValues[1]);
        		}
        		onBodyRotateToTarget(iBearing, iSpeed);
        	}
        	else if(sCmd.equals("turret_set"))
        	{
        		short iLow = 0, iHigh = 0;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("low"))
        				iLow = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high"))
        				iHigh = Integer.getInteger(sValues[1]).shortValue();
        		}
        		onTurretSetPosition(iLow, iHigh);
        	}
        	else if(sCmd.equals("turret_scan_point"))
        	{
        		short iLow = 0, iHigh = 0;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("low"))
        				iLow = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high"))
        				iHigh = Integer.getInteger(sValues[1]).shortValue();
        		}
        		onTurretScanPosition(iLow, iHigh);
        	}
        	else if(sCmd.equals("turret_scan_line"))
        	{
        		short iLowBeg=0, iLowEnd=0, iLowStep=0, iHigh=0;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("low_beg"))
        				iLowBeg = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("low_end"))
        				iLowEnd = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("low_step"))
        				iLowStep = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high"))
        				iHigh = Integer.getInteger(sValues[1]).shortValue();
        		}
        		onTurretScanLine(iLowBeg, iLowEnd, iLowStep, iHigh);
        	}
        	else if(sCmd.equals("turret_scan_zone"))
        	{
        		short iLowBeg=0, iLowEnd=0, iLowStep=0, iHighBeg=0, iHighEnd=0, iHighStep=0;
        		String [] sParams = sOps.split("\\,");
        		for(String sParam : sParams)
        		{
        			String [] sValues = sParam.split("\\=");
        			if(sValues[0].equals("low_beg"))
        				iLowBeg = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("low_end"))
        				iLowEnd = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("low_step"))
        				iLowStep = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high_beg"))
        				iHighBeg = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high_end"))
        				iHighEnd = Integer.getInteger(sValues[1]).shortValue();
        			else if(sValues[0].equals("high_step"))
        				iHighStep = Integer.getInteger(sValues[1]).shortValue();
        		}
        		onTurretScanZone(iLowBeg, iLowEnd, iLowStep, iHighBeg, iHighEnd, iHighStep);
        	}
        	else if(sCmd.equals("position_ask"))
        	{
    			onAskPosition();
        	}
        	else if(sCmd.equals("camera_ask"))
        	{
        		if(sOps.equals("image"))
        			onAskCameraImage();
        	}
    	}
    }

	@Override
	public void onHello(String sVersion, String sControllerName) {
	}
	@Override
	public void onAskVersion() {
	}
	@Override
	public void onAskSetting() {
	}
	@Override
	public void onAskSettingBody() {
	}
	@Override
	public void onAskSettingTurret() {
	}
	@Override
	public void onAskSettingRay() {
	}
	@Override
	public void onSetSettingRayCount(int iCount) {
	}
	@Override
	public void onSetSettingRayDelay(int iDelay) {
	}
	@Override
	public void onSetSettingRaySensors(int sSensors) {
	}
	@Override
	public void onSetSettingRayPrecision(float fPrecision) {
	}
	@Override
	public void onSetSettingRay(int iCount, int iDelay, int sSensors,
			float fPrecision) {
	}
	@Override
	public void onAskStatus() {
	}
	@Override
	public void onAskStatusSensors() {
	}
	@Override
	public void onAskStatusTurret() {
	}
	@Override
	public void onAskStatusBody() {
	}
	@Override
	public void onSetSendStatus(boolean bSet) {
	}
	@Override
	public void onReset() {
	}
	@Override
	public void onResetBody() {
	}
	@Override
	public void onResetTurret() {
	}
	@Override
	public void onBodyFront(int iSpeed) {
	}
	@Override
	public void onBodyBack(int iSpeed) {
	}
	@Override
	public void onBodyLeft(int iSpeed) {
	}
	@Override
	public void onBodyRight(int iSpeed) {
	}
	@Override
	public void onBodyMoveToTarget(int iSpeed, boolean bFront, int iLen) {
	}
	@Override
	public void onBodyRotateToTarget(int iSpeed, int iBearing) {
	}
	@Override
	public void onTurretSetPosition(short low, short high) {
	}
	@Override
	public void onTurretScanPosition(short low, short high) {
	}
	@Override
	public void onTurretScanLine(short low_beg, short low_end, short low_step,
			short high) {
	}
	@Override
	public void onTurretScanZone(short low_beg, short low_end, short low_step,
			short high_beg, short high_end, short high_step) {
	}
	@Override
	public void onAskPosition() {
	}
	@Override
	public void onAskCameraImage() {
	}
	@Override
	public void onAskVibrate(long [] datas)
	{
	}
}
